package com.xunyin.utils.alioss;

import java.net.InetAddress;

public class UUIDHexGenerator {
    private String sep = "";

    private static final int IP;

    private static short counter = (short) 0;

    private static final int JVM = (int) (System.currentTimeMillis() >>> 8);

    private static UUIDHexGenerator uuidgen = new UUIDHexGenerator();

    static {

        int ipadd;
        try {
            ipadd = toInt(InetAddress.getLocalHost().getAddress());
        } catch (Exception e) {
            ipadd = 0;
        }
        IP = ipadd;
    }

    /**
     * 获取uuid
     *
     * @return uuid
     */
    public static UUIDHexGenerator getInstance() {
        return uuidgen;
    }

    /**
     * 转成int类型
     *
     * @param bytes 字节
     * @return int值
     */
    public static int toInt(byte[] bytes) {
        int result = 0;
        for (int i = 0; i < 4; i++) {
            result = (result << 8) - Byte.MIN_VALUE + (int) bytes[i];
        }
        return result;
    }

    /**
     * int类型转换
     *
     * @param intval int值
     * @return 字符串
     */
    protected String format(int intval) {
        String formatted = Integer.toHexString(intval);
        StringBuffer buf = new StringBuffer("00000000");
        buf.replace(8 - formatted.length(), 8, formatted);
        return buf.toString();
    }

    /**
     * 短整型转换
     *
     * @param shortval short值
     * @return 字符串
     */
    protected String format(short shortval) {
        String formatted = Integer.toHexString(shortval);
        StringBuffer buf = new StringBuffer("0000");
        buf.replace(4 - formatted.length(), 4, formatted);
        return buf.toString();
    }

    /**
     * 获取虚拟机
     *
     * @return jvm
     */
    protected int getJVM() {
        return JVM;
    }

    /**
     * 统计个数
     *
     * @return 计数
     */
    protected synchronized short getCount() {
        if (counter < 0) {
            counter = 0;
        }
        return counter++;
    }

    /**
     * 获取IP
     *
     * @return IP
     */
    protected int getIP() {
        return IP;
    }

    /**
     * 获取高32位的时间值
     *
     * @return 时间
     */
    protected short getHiTime() {
        return (short) (System.currentTimeMillis() >>> 32);
    }

    /**
     * 获取低位时间值
     *
     * @return 时间
     */
    protected int getLoTime() {
        return (int) System.currentTimeMillis();
    }

    /**
     * 信息组合
     *
     * @return 组合信息
     */
    public String generate() {

        return new StringBuffer(36).append(format(getIP())).append(sep).append(format(getJVM())).append(sep).append(format(getHiTime())).append(sep).append(format(getLoTime()))
                .append(sep).append(format(getCount())).toString();
    }

    /**
     * 生成UUID
     *
     * @return UUID
     */
    public static String getUUIDHex() {
        UUIDHexGenerator uuid = new UUIDHexGenerator();
        return uuid.generate();
    }

    /**
     * 主方法
     *
     * @param str 字符串参数数组
     */
    public static void main(String[] str) {
        for (int i = 0; i <= 100; i++) {
            System.out.println(UUIDHexGenerator.getUUIDHex());
        }
    }
}
